package com.sdkj.dspservice.common.utils.dsp;

import com.sdkj.dspservice.common.base.entity.BaseResult;
import com.sdkj.dspservice.common.config.ProjectConfig;
import com.sdkj.dspservice.common.constant.HttpStatus;
import com.sdkj.dspservice.common.utils.*;
import com.sdkj.dspservice.common.utils.okhttp3.OkHttpUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * cdp工具类
 */
public class CdpUtils {

    private static ProjectConfig projectConfig = SpringContextHolder.getBean(ProjectConfig.class);
    // TODO: 2021/2/24 后期转换为本地缓存去做
    private static String token;
    private static String ip;
    private static String clientSecret;

    static {
        ip = projectConfig.getCdpApiIp();
        clientSecret = projectConfig.getCdpClientSecret();
    }

    /**
     * 获取AccessToken
     * @return
     */
    public static void getAccessToken(){
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("grant_type", "client_credentials");
        paramsMap.put("client_id", "api");
        paramsMap.put("client_secret", clientSecret);
        paramsMap.put("scope", "system");
        BaseResult result = HttpUtils.post(ip + "/accounts/oauth/access_token", paramsMap);
        token = JsonUtils.getValue(String.valueOf(result.getData()), "access_token");
    }

    /**
     * 执行graphql
     * @param jsonParams json参数
     * @return
     */
    public static BaseResult graphql(String jsonParams){
        if(StringUtils.isBlank(token)){
            getAccessToken();
        }
        System.out.println("**********************AccessToken：" + token + "***********************");
        BaseResult result = HttpUtils.post(ip + "/graphql", jsonParams, new HashMap<String, String>(){{put("Authorization", token);}});
        // 取出graphql执行的状态码
        int code = result.getCode();
        // 如果状态码为401，则重新获取token，重新请求graphql
        if(HttpStatus.STATUS_401 == code){
            getAccessToken();
            result = HttpUtils.post(ip + "/graphql", jsonParams, new HashMap<String, String>(){{put("Authorization", token);}});
        }
        System.out.println("**********************resultData：" + result.getData() + "***********************");
        return result;
    }

    /**
     * 发送手机短信
     * @param mobile    手机号
     * @param content   内容
     */
    public static void sendMobileMessage(String mobile, String content){
        // 获取运行模式
        String springProfilesActive = SpringContextHolder.getApplicationContext().getEnvironment().getActiveProfiles()[0];
        if(!"dev".equals(springProfilesActive)){
            // 非正式环境才发短信
            String json = "{\"msgType\":\"sendSmsToAll\",\"createTime\":\""+ DateUtils.getDate() + "\",\"version\":\"1.1.1\",\"content\":{\"cellphone\":\"" + mobile + "\",\"accessToken\":\"a7a7c745037b1bd37ffc3ef28bc44834\",\"accessId\":\"SM20160616\",\"type\":\"smart\",\"params\":\"" + ("【行为数据平台】" + content) + "\",\"templateType\":\"gj\"}}";
            Map<String, String> headersMap = new HashMap<>();
            headersMap.put("Content-Type", "application/json");
            headersMap.put("cellphone", mobile);
            headersMap.put("interfaceCode", "1.48.1");
            String url = "http://10.226.5.135/interface/MsgPort";
            OkHttpUtils.postJsonParams(url, json, headersMap);
        }
    }

}
